package com.rj.bd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.rj.bd.entity.Admin;
import com.rj.bd.service.IAdminService;

import redis.clients.jedis.Jedis;

/**
 * @author FTY
 * @dasc
 * @time 2021-05-31 14:09
 */


@RestController
public class AdminController {
	
	
	//使用win10本机的redis
	Jedis Jedis = new Jedis("127.0.0.1",6379);
	
	@Autowired
	public IAdminService adminService;//自动装配
	
	
	
	/**
	 * @desc 管理员登陆
	 */
	@RequestMapping(value="admin/login",produces={"application/json;charset=UTF-8"})
	@ResponseBody
	@CrossOrigin(origins="*",maxAge=3600)
	public Map<String, Object> login(@Param("anumber")String anumber,@Param("apassword")String apassword,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String,Object>();
		System.out.println("AdminController-------login()-----登录");
		System.out.println("账号"+anumber);
		System.err.println("密码"+apassword);
	 	String yanzhengma = request.getParameter("yanzhengma");
		if (anumber==null || anumber.equals("")) {
			map.put("login", 0);
			map.put("msg", "请输入账号");
			return map;
		}
		if (apassword==null ||apassword.equals("")) {
			map.put("login",1);
			map.put("msg", "请输入密码");
			return map;
		}
		if (yanzhengma==null ||yanzhengma.equals("")) {
			map.put("login", 2);
			map.put("msg", "验证码为空。请重新获取");
			return map;
		}
		
		
		
		Admin admin = adminService.login(anumber,apassword);

		map.put("login", 3);
		map.put("code", 0);
		map.put("data", admin);
		System.out.println("账号:"+anumber+"\t"+"密码:"+apassword);
		return map;
			
	}
	
	
	
	
	
	/**
	 * @desc  管理员模块查询全部
	 * @param request
	 * @return
	 */
	@RequestMapping(value="admin/queryAll",produces={"application/json;charset=UTF-8"})
	@ResponseBody
	@CrossOrigin(origins = "*",maxAge=3600)
	public Map<String, Object> queryAll(HttpServletRequest request){
		System.out.println("AdminController------->queryAll()");
		
		Map<String, Object> map = new HashMap<String,Object>();
		List<Admin> list = adminService.queryAll();
		map.put("code", 0);
		map.put("msg", "查询成功");
		map.put("count",list.size());
		map.put("data", list);
		return map;
	}
	
	

	
	
}


